﻿using Microsoft.AspNetCore.Diagnostics;
using PetAdoptApi.Extensions;
using static System.Net.Mime.MediaTypeNames;

namespace PetAdoptApi.Services;

public class ErrorHandler
{
    public static Action<IApplicationBuilder> ConfigExceptionhandler = exceptionHandlerApp =>
    {
        exceptionHandlerApp.Run(async context =>
        {
            context.Response.ContentType = Application.Json;
            var exceptionHandlerPathFeature =
             context.Features.Get<IExceptionHandlerPathFeature>();
            if (exceptionHandlerPathFeature?.Error is Exception err)
            {
                await context.WriteResErrorAsync(err.Message);
            }
            else
            {
                await context.Response.WriteAsync("An exception was thrown.");
            }
        });
    };
}
